/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import service.impl.ContendorServiceImpl;
import entity.Contendor;

@Controller
public class ContendorController {
    
    private ContendorServiceImpl ContendorServiceImpl;

    public void setContendorServiceImpl(ContendorServiceImpl ContendorServiceImpl) {
        this.ContendorServiceImpl = ContendorServiceImpl;
    }
    
    @RequestMapping(params="accion=qry")
    public ModelAndView query(){
        List<Contendor> list = ContendorServiceImpl.buscarTodos();
        ModelAndView mav = new ModelAndView("ListarContendor");
        mav.addObject("list",list);
        Contendor Contendor = new Contendor();
        mav.addObject("Contendor",Contendor);
        return mav;
    }
    
    @RequestMapping(params="accion=fnd")
    public ModelAndView find(@ModelAttribute Contendor Contendor){
        List<Contendor> list = ContendorServiceImpl.buscar(Contendor);
        ModelAndView mav = new ModelAndView("ListarContendor");
        mav.addObject("list",list);
        Contendor Contendor1 = new Contendor();
        mav.addObject("Contendor",Contendor1);
        return mav;
    }
    
    @RequestMapping(params="accion=ejecutar")
    public ModelAndView ejecutar(HttpServletRequest request){
        String idIni = request.getParameter("id");
        Double id = Double.parseDouble(idIni);
        List<Contendor> list1 = ContendorServiceImpl.buscarNivel1(id);
        List<Contendor> list2 = ContendorServiceImpl.buscarNivel2(id);
        List<Contendor> list3 = ContendorServiceImpl.buscarNivel3(id);
        List<Contendor> list4 = ContendorServiceImpl.buscarNivel4(id);
        List<Contendor> list5 = ContendorServiceImpl.buscarNivel5(id);
        List<Contendor> list6 = ContendorServiceImpl.buscarNivel6(id);
        List<Contendor> list7 = ContendorServiceImpl.buscarNivel7(id);
        
        ModelAndView mav = new ModelAndView("EjecutarTorneo");
        mav.addObject("list1",list1);
        mav.addObject("list2",list2);
        mav.addObject("list3",list3);
        mav.addObject("list4",list4);
        mav.addObject("list5",list5);
        mav.addObject("list6",list6);
        mav.addObject("list7",list7);
        return mav;
    }
    
    @RequestMapping(params="accion=ejecutarReporte")
    public ModelAndView ejecutarReporte(HttpServletRequest request){
        String idIni = request.getParameter("id");
        Double id = Double.parseDouble(idIni);
        List<Contendor> list1 = ContendorServiceImpl.buscarNivel1(id);
        List<Contendor> list2 = ContendorServiceImpl.buscarNivel2(id);
        List<Contendor> list3 = ContendorServiceImpl.buscarNivel3(id);
        List<Contendor> list4 = ContendorServiceImpl.buscarNivel4(id);
        List<Contendor> list5 = ContendorServiceImpl.buscarNivel5(id);
        List<Contendor> list6 = ContendorServiceImpl.buscarNivel6(id);
        List<Contendor> list7 = ContendorServiceImpl.buscarNivel7(id);
        
        ModelAndView mav = new ModelAndView("EjecutarTorneoReporte");
        mav.addObject("list1",list1);
        mav.addObject("list2",list2);
        mav.addObject("list3",list3);
        mav.addObject("list4",list4);
        mav.addObject("list5",list5);
        mav.addObject("list6",list6);
        mav.addObject("list7",list7);
        return mav;
    }
    
    @RequestMapping(params="accion=ins1")
    public ModelAndView insert1(){
        ModelAndView mav = new ModelAndView("IngresarContendor");
        Contendor Contendor = new Contendor();
        mav.addObject("Contendor",Contendor);
        return mav;
    }
    
    @RequestMapping(params="accion=ins2")
    public ModelAndView insert2(@ModelAttribute Contendor Contendor){
        Integer result = ContendorServiceImpl.insertar(Contendor);
        result = 0;
        List<Contendor> list = ContendorServiceImpl.buscarTodos();
        ModelAndView mav = new ModelAndView("ListarContendor");
        mav.addObject("list",list);
        Contendor Contendors = new Contendor();
        mav.addObject("Contendor",Contendors);
        return mav;
    }
    
    @RequestMapping(params="accion=del")
    public ModelAndView delete(HttpServletRequest request){
        String id = request.getParameter("id");
        if(id!=null){
            ContendorServiceImpl.eliminar(Double.valueOf(id));
        }
        List<Contendor> list = ContendorServiceImpl.buscarTodos();
        ModelAndView mav = new ModelAndView("ListarContendor");
        mav.addObject("list",list);
        Contendor Contendors = new Contendor();
        mav.addObject("Contendor",Contendors);
        return mav;
    }
    
    @RequestMapping(params="accion=upd1")
    public ModelAndView update1(HttpServletRequest request){
        String id = request.getParameter("id");
        Contendor Contendor = null;
        if(id != null){
            Contendor = ContendorServiceImpl.buscarPorId(Double.valueOf(id));
        }
        List<Contendor> list = ContendorServiceImpl.buscarTodos();
        ModelAndView mav = new ModelAndView("EditarContendor");
        mav.addObject("list",list);
        mav.addObject("Contendor",Contendor);
        return mav;
    }
    
    @RequestMapping(params="accion=upd2")
    public ModelAndView update2(@ModelAttribute Contendor Contendor){
        ContendorServiceImpl.actualizar(Contendor);
        List<Contendor> list = ContendorServiceImpl.buscarTodos();
        ModelAndView mav = new ModelAndView("ListarContendor");
        mav.addObject("list",list);
        Contendor Contendors = new Contendor();
        mav.addObject("Contendor",Contendors);
        return mav;
    }
    
    @RequestMapping(params="accion=list1")
    public ModelAndView execList1(HttpServletRequest request){
        String idtor = request.getParameter("idtor");
        String idcon = request.getParameter("idcon");
        String codGanador = request.getParameter("codgad");
        Double idtor2 = Double.parseDouble(idtor);
        Double idcon2 = Double.parseDouble(idcon);
        Integer codGanador2 = Integer.parseInt(codGanador);
        ContendorServiceImpl.pasarRonda(idtor2,idcon2,codGanador2);
        List<Contendor> list1 = ContendorServiceImpl.buscarNivel1(idtor2);
        List<Contendor> list2 = ContendorServiceImpl.buscarNivel2(idtor2);
        List<Contendor> list3 = ContendorServiceImpl.buscarNivel3(idtor2);
        List<Contendor> list4 = ContendorServiceImpl.buscarNivel4(idtor2);
        List<Contendor> list5 = ContendorServiceImpl.buscarNivel5(idtor2);
        List<Contendor> list6 = ContendorServiceImpl.buscarNivel6(idtor2);
        List<Contendor> list7 = ContendorServiceImpl.buscarNivel7(idtor2);
        ModelAndView mav = new ModelAndView("EjecutarTorneo");
        mav.addObject("list1",list1);
        mav.addObject("list2",list2);
        mav.addObject("list3",list3);
        mav.addObject("list4",list4);
        mav.addObject("list5",list5);
        mav.addObject("list6",list6);
        mav.addObject("list7",list7);
        return mav;
    }
}
